/**
 * Title: ClearHolidayLimitListHandler.java
 * Description: 
 * @author 相建彬 Email:tjxiangjianbin@kingdee.com
 * @date 2019-8-5
 * @version 1.0
 */
package com.kingdee.shr.custom.handler.holiday;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.IMetaDataLoader;
import com.kingdee.bos.metadata.MetaDataLoaderFactory;
import com.kingdee.bos.metadata.entity.EntityObjectInfo;
import com.kingdee.bos.util.BOSObjectType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.base.syssetting.web.handler.ListHandler;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;

/**
 * 
 * @copyright 天津金蝶软件有限公司 <br>
 *            Title: ClearHolidayLimitListHandler <br>
 *            Description:
 * @author 相建彬 Email:tjxiangjianbin@kingdee.com
 * @date 2019年8月15日
 */
public class ClearHolidayLimitListHandler extends ListHandler {
    private static Logger logger = Logger.getLogger(ClearHolidayLimitListHandler.class);

    private Context ctx;

    public ClearHolidayLimitListHandler(Context ctx) {
        this.ctx = ctx;
    }

    public ClearHolidayLimitListHandler() {
        this.ctx = SHRContext.getInstance().getContext();
    }

    /**
     * 
     * <p>
     * Title: clearHoliday
     * </p>
     * <p>
     * Description:清除假期额度
     * </p>
     * 
     * @param request
     * @param response
     * @param modelMap
     * @throws ShrWebBizException
     */
    public void clearHolidayAction(HttpServletRequest request, HttpServletResponse response,
            ModelMap modelMap) throws ShrWebBizException {
        String ids = request.getParameter("billId");
        logger.info("选择的清除额度的数据id为" + ids);
        String[] billIds = ids.split(",");
        if (billIds.length <= 0) {
            throw new ShrWebBizException(" 清除额度失败！id数量为" + billIds.length);
        }
        String tableName = getTableNameByBillId(billIds[0]);
        for (int i = 0; i < billIds.length; i++) {
            if (StringUtils.isBlank(tableName) || StringUtils.isBlank(billIds[i])) {
                throw new ShrWebBizException(" 清除额度失败！id 或表名未知");
            }
            logger.info("*************选择的清除额度的数据id为****************" + billIds[i]);
            String sql = " update " + tableName
                    + " set FStandardLimit = '0',  FRealLimit = '0' ,FUsedLimit='0', FRemainLimit='0' where fid = '"
                    + billIds[i] + "'";
            try {
                DbUtil.execute(ctx, sql);
                JSONUtils.writeJson(response, modelMap);
            } catch (BOSException e) {
                e.printStackTrace();
                logger.error("清除额度失败！" + e);
            } catch (SHRWebException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 
     * <p>
     * Title: getTableNameByBillId
     * </p>
     * <p>
     * Description: 获取表名
     * </p>
     * 
     * @param billId
     * @return tableName
     */
    private String getTableNameByBillId(String billId) {
        if (StringUtils.isNotEmpty(billId)) {
            BOSUuid uuid = BOSUuid.read(billId);
            BOSObjectType bosType = uuid.getType();
            IMetaDataLoader metadataloader = MetaDataLoaderFactory.getRemoteMetaDataLoader();
            EntityObjectInfo entity = metadataloader.getEntity(bosType);
            String tableName = entity.getTable().getName();

            return tableName;
        }
        return null;
    }
}
